﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

public static class Global
{
     static bool vlez1;
     static String g1;
     static String o1;
     static int per1;
     static double d1;
     static double tv1;

     public static string G1
     {
         get
         {
             return g1;
         }
         set
         {
             g1 = value;
         }
     }
     public static string O1
     {
         get
         {
             return o1;
         }
         set
         {
             o1 = value;
         }
     }
     public static bool VLEZ1
     {
         get
         {
             return vlez1;
         }
         set
         {
             vlez1 = value;
         }
     }
     public static int PER1
     {
         get
         {
             return per1;
         }
         set
         {
             per1 = value;
         }
     }
     public static double D1
     {
         get
         {
             return d1;
         }
         set
         {
             d1 = value;
         }
     }
     public static double TV1
     {
         get
         {
             return tv1;
         }
         set
         {
             tv1 = value;
         }
     }
    

}

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String[] gradovi = { "Скопје","Куманово","Свети Николе","Крива Паланка","Делчево","Велес","Штип","Берово","Неготино","Струмица","Гевгелија","Прилеп","Битола","Крушево","Ресен","Охрид","Струга","Дебар","Кичево","Македонски Брод","Гостивар","Тетово"};
        String[] prvaKlasa = { "Скопје", "Битола", "Охрид" };
        String[] vtoraKlasa = { "Куманово", "Штип", "Струмица", "Прилеп", "Крушево", "Тетово", "Гевгелија" };
        String[] tretaKlasa = { "Крива Паланка", "Свети Николе", "Делчево", "Велес", "Берово", "Неготино", "Ресен", "Струга", "Дебар", "Кичево", "Македонски Брод", "Гостивар" };

        bool vlez = Global.VLEZ1;
        String g = Global.G1;
        String o = Global.O1;
        int per = Global.PER1;
        double d = Global.D1;
        double tv = Global.TV1;

        if((grad.SelectedValue!="Изберете")&&(period.SelectedValue!="Изберете")&&((izb.SelectedValue=="Парцела")||(izb.SelectedValue=="Објект"))){
            inf.Text = "";
            Boolean p = false, v = false, t = false;
            int i1 = 0, i2 = 0, i3 = 0;
            g = grad.SelectedValue;
            Global.G1 = g;
            per = Convert.ToInt32(period.SelectedValue);
            Global.PER1 = per;
            while (i1 < prvaKlasa.Length)
            {
                if (prvaKlasa[i1] == grad.SelectedValue)
                    p = true;
                i1++;
            }
            while (i2 < vtoraKlasa.Length)
            {
                if (vtoraKlasa[i2] == grad.SelectedValue)
                    v = true;
                i2++;
            }
            while (i3 < tretaKlasa.Length)
            {
                if (tretaKlasa[i3] == grad.SelectedValue)
                    t = true;
                i3++;
            }
            double dava, zima;
            double tmp = (Convert.ToDouble(period.SelectedValue)) ;
            double kam = tmp / 25;

            if ((izb.SelectedValue == "Објект")&&(obj.SelectedValue != "Изберете"))
            {
                vlez = true;
                Global.VLEZ1 = vlez;
                o = obj.SelectedValue;
                Global.O1 = o;
                if (p)
                {
                    if (obj.SelectedValue == "Куќа")
                    {                        
                        dava = 50/2 + 6; // vrednosta na kukjata da se izgradi e 50    +6 bidejkji e vo prva klasa
                        zima = dava + kam * dava;
                        sum.Text = "" + dava;
                        vrt.Text = "" + zima;
                        d = dava;
                        tv = zima;
                        Global.D1 = d;
                        Global.TV1 = tv;
                    }
                    if (obj.SelectedValue == "Зграда")
                    {
                        dava = 200/2+20; // vrednosta na zgrada da se izgradi e 200      +20 bidekji e vo prva klasa
                        zima = dava + kam * dava;
                        sum.Text = "" + dava;
                        vrt.Text = "" + zima;
                        d = dava;
                        tv = zima;
                        Global.D1 = d;
                        Global.TV1 = tv;
                    }

                }
                if (v)
                {
                    if (obj.SelectedValue == "Куќа")
                    {
                        dava = 50 / 2 + 3; // vrednosta na kukjata da se izgradi e 50    +3 bidejkji e vo vora klasa
                        zima = dava + kam * dava;
                        sum.Text = "" + dava;
                        vrt.Text = "" + zima;
                        d = dava;
                        tv = zima;
                        Global.D1 = d;
                        Global.TV1 = tv;
                    }
                    if (obj.SelectedValue == "Зграда")
                    {
                        dava = 200 / 2 + 10; // vrednosta na zgrada da se izgradi e 200      +10 bidekji e vo vora klasa
                        zima = dava + kam * dava;
                        sum.Text = "" + dava;
                        vrt.Text = "" + zima;
                        d = dava;
                        tv = zima;
                        Global.D1 = d;
                        Global.TV1 = tv;
                    }

                }
                if (t)
                {
                    if (obj.SelectedValue == "Куќа")
                    {
                        dava = 50 / 2; // vrednosta na kukjata da se izgradi e 50    
                        zima = dava + kam * dava;
                        sum.Text = "" + dava;
                        vrt.Text = "" + zima;
                        d = dava;
                        tv = zima;
                        Global.D1 = d;
                        Global.TV1 = tv;
                    }
                    if (obj.SelectedValue == "Зграда")
                    {
                        dava = 200 / 2; // vrednosta na zgrada da se izgradi e 200
                        zima = dava + kam * dava;
                        sum.Text = "" + dava;
                        vrt.Text = "" + zima;
                        d = dava;
                        tv = zima;
                        Global.D1 = d;
                        Global.TV1 = tv;
                    }
                }           

            }
            else if ((izb.SelectedValue == "Парцела")&&(obj.SelectedValue == "Изберете"))
            {
                o = "Парцела";
                Global.O1 = o;
                vlez = true;
                Global.VLEZ1 = vlez;
                if(p){
                    dava = 30;
                    zima = dava + kam*dava;
                    sum.Text = "" + dava;
                    vrt.Text = "" + zima;
                    d = dava;
                    tv = zima;
                    Global.D1 = d;
                    Global.TV1 = tv;
                }
                if (v){
                    dava = 25;
                    zima = dava + kam * dava;
                    sum.Text = "" + dava;
                    vrt.Text = "" + zima;
                    d = dava;
                    tv = zima;
                    Global.D1 = d;
                    Global.TV1 = tv;
                }
                if (t){
                    dava = 20;
                    zima = dava + kam * dava;
                    sum.Text = "" + dava;
                    vrt.Text = "" + zima;
                    d = dava;
                    tv = zima;
                    Global.D1 = d;
                    Global.TV1 = tv;
                }

            }
            else{
                     inf.Text = "Не се сите полиња пополнети!!! Проверете го полето ОБЈЕКТ? ";
                     sum.Text = "";
                     vrt.Text = "";
                     vlez = false;
                     Global.VLEZ1 = vlez;
            }

        }else{
            inf.Text = "Не се сите полиња пополнети!!!";
            sum.Text = "";
            vrt.Text = "";
            vlez = false;
            Global.VLEZ1 = vlez;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        bool vlez = Global.VLEZ1;
        String g = Global.G1;
        String o = Global.O1;
        int per = Global.PER1;
        double d = Global.D1;
        double tv = Global.TV1;

        if (vlez)
        {
            string conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection con = new OleDbConnection(conString);
           
            con.Open();

            string sql = "insert into Glavna (Grad, Objekt, Period, Dava, TrebaDaVrati) values (?,?,?,?,?)";

            OleDbCommand cmd = new OleDbCommand(sql, con);

            cmd.Parameters.AddWithValue("?", g);
            cmd.Parameters.AddWithValue("?", o);
            cmd.Parameters.AddWithValue("?", per);
            cmd.Parameters.AddWithValue("?", d);
            cmd.Parameters.AddWithValue("?", tv);

            cmd.ExecuteNonQuery();
            con.Close();
            
            inf.Text = "Кредитот е одобрен";
        }
        else
        {
            inf.Text = "Не се сите полиња пополнети!!!";
            sum.Text = "";
            vrt.Text = "";
        }


    }
}
